<?php
$txt=file_get_contents('php://input');
logfile(date('Y-m-d H:i:s').'---'.$txt,'nuomi_cancel_ewm_log.txt');

function logfile($txt,$path){
	$fp = fopen($path,"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}

$s1=new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));
$ac='123545'; //百度糯米接口账号
$pw='fa23a74898ad0ca49b6a9ae82e9b3fb8';

parse_str($txt,$date); //转换数据

$custId = '123545'; //二维码商帐号
$p_encode = '123545'; //二维码商密钥
$time = $date['time']; //获取时间戳
$encode = md5($p_encode.$time.$custId); //加密签名

if($date['encode'] == $encode) //判断签名是否相等
{
	$orderId = $date['orderId'];
	$thirdOrderId =$date['thirdOrderId'];
	$thirdTicketId =$date['thirdTicketId'];
	$arr= array();
	$cancel_array = array("ac"=>$ac,"pw"=>$pw,"in0"=>$orderId,"in1"=>0,"in2"=>"");
	
	//这里是取消订单程序
	$xml = $s1->__soapCall("Order_Change_Pro",$cancel_array);
	$result=simpleXML_load_string($xml);
	$str= array();
	try
 	{
		if ($result->Rec->UUerrorcode){
			$txt_json=json_encode($cancel_array);
			$txt_json.='|'.$result->Rec->UUerrorcode;
			err($txt_json,'nm_debug.txt');
			echo '{"msg":"failed", "data":{"orderId":"'.$orderId.'","thirdOrderId":"'.$thirdOrderId.'","thirdTicketId":"'.$thirdTicketId.'"}}';
/*			$str["msg"] = "failed";
            $str["data"]["orderid"] = "";
			echo json_encode($str);
*/			throw new Exception(json_encode('{"msg":"failed", "data":{"orderId":"'.$orderId.'","thirdOrderId":"'.$thirdOrderId.'","thirdTicketId":"'.$thirdTicketId.'"}}'));
				
		}else{
		//成功
/*			$str["msg"] = "success";
            $str["data"]["orderid"] = $orderId;
			$str["data"]["thirdOrderId"] =$thirdOrderId;
			$str["data"]["thirdTicketId"]=$thirdTicketId;
			echo json_encode($str);
*/			echo '{"msg":"success", "data":{"orderId":"'.$orderId.'","thirdOrderId":"'.$thirdOrderId.'","thirdTicketId":"'.$thirdTicketId.'"}}';
		}
	 }
	catch (Exception $e)
	 {
	 echo $e->errorMessage();
	 //echo '{"msg":"failed", "data":{"orderId":"","thirdOrderId":"'.$thirdOrderId.'","thirdTicketId":"'.$thirdTicketId.'"}}';
	 }
	
}

function err($txt,$path){
	$fp = fopen($path,"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}

?>